package com.example.clientbwic.utils;

import com.example.clientbwic.entity.Bid;
import com.example.clientbwic.entity.Bwic;
import com.example.clientbwic.entity.BwicRespose;
import com.example.clientbwic.service.bid.BidService;
import com.example.clientbwic.service.bid.BidServiceImpl;
import com.example.clientbwic.service.bwic.BwicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 86150
 * @Date: 2024/4/14
 * @Description: com.example.clientbwic.utils
 */
@Component
public class BwicToBwicResponse {
    private static BidService bidService;
    private static List<BwicRespose> bwicResposeList = new ArrayList<>();
    public BwicToBwicResponse(BidService service){
        BwicToBwicResponse.bidService = service;
    }
    public static List<BwicRespose> convert(List<Bwic> bwics){

        for(Bwic bwic : bwics){
            BwicRespose bwicRespose = new BwicRespose();

            bwicRespose.setBwicId(bwic.getBwic_id());
            bwicRespose.setCusip(bwic.getCusip());
            bwicRespose.setPosition(bwic.getPosition());
            bwicRespose.setPrice(bwic.getMarket_price());
            bwicRespose.setDueDate(bwic.getDue_date());
            bwicRespose.setMarketValue(bwic.getStarting_amount());
            bwicRespose.setOverDue(timeCompare(getCurrentTime(), bwic.getDue_date()));

            bwicRespose.setTotalBids(bidService.getBidsCountByBwicId(Long.valueOf(bwic.getBwic_id())));

//            bwicRespose.setBidRank();
            Bid successBidByBwicId = bidService.getSuccessBidByBwicId(Long.valueOf(bwic.getBwic_id()));
            bwicRespose.setWinner(successBidByBwicId == null ? null :successBidByBwicId.getClient_id());
            Integer a = Integer.valueOf(bwic.getPosition().trim());
            String trim = bwic.getMarket_price().trim();
            Double b = Double.valueOf(trim);
            Double i = a * b;
            String s = String.valueOf(i);
            bwicRespose.setBidMarketValue(s);
            bwicResposeList.add(bwicRespose);
        }

        return bwicResposeList;
    }
    /*获得当前时间*/
    public static String getCurrentTime(){
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    /*时间比大小*/
    public static Boolean timeCompare(String t1,String t2){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        Calendar c1=Calendar.getInstance();
        Calendar c2=Calendar.getInstance();
        try {
            c1.setTime(formatter.parse(t1));
            c2.setTime(formatter.parse(t2));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int result=c1.compareTo(c2);
        return result > 0 ? true : false;
    }

}

